package com.zxl.live.tools.base;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuBuilder;
import android.view.Menu;

/**
 * 基础Activity
 * <p>
 * Created by Administrator on 2016/4/21.
 * <br/>
 */
public class BaseAppCompatActivity extends AppCompatActivity implements BaseAppCompatFragment.BackHandledInterface {

    private boolean mIsMainActivity = false;

    protected BaseAppCompatFragment mBackHandedFragment;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (MenuBuilder.class.isInstance(menu)) {
            ((MenuBuilder) menu).setOptionalIconsVisible(true);
        }
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 设置主Activity
     *
     * @param mIsMainActivity
     */
    public void setMainActivity(boolean mIsMainActivity) {
        this.mIsMainActivity = mIsMainActivity;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mIsMainActivity) {
            ActivityLifecycle.killSelf();
        }
    }

    @Override
    public void setSelectedFragment(BaseAppCompatFragment selectedFragment) {
        mBackHandedFragment = selectedFragment;
    }

    @Override
    public void onBackPressed() {
        if (null != mBackHandedFragment) {
            if (!mBackHandedFragment.onBackPressed()) {
                if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                    super.onBackPressed();
                } else {
                    getSupportFragmentManager().popBackStack();
                }
            }
        } else {
            super.onBackPressed();
        }
    }
}
